<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="政策主题" prop="policyThemes">
            <el-input
              v-model="queryParams.policyThemes"
              placeholder="请输入政策主题"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <div style="width: 100%;height: 90vh;overflow:auto; margin-bottom: 30%;padding-bottom: 18%;position: relative;">
        <el-table :row-style="{ height: '30px' }" :cell-style="{ padding: '0px' }" height="450" border   v-loading="loading" :data="releaseList" highlight-current-row>
          <el-table-column label="状态" align="center" prop="status"/>
          <el-table-column label="创建人" align="center" prop="creationName"  :show-overflow-tooltip="true" width="100"/>
          <el-table-column label="创建时间" align="center" prop="creationTime" width="120"  :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.creationTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="政策主题" align="center" prop="policyThemes"  :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <template>
                <!-- 使用按钮作为可点击元素，并绑定点击事件 -->
                <el-button @click="handleSelect(scope.row,scope.$index)" type="text">
                  {{ scope.row.policyThemes }}
                </el-button>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="原文网址" align="center" prop="website"  :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <template>
                <!-- 使用按钮作为可点击元素，并绑定点击事件 -->
                <el-button @click="handleClick(scope.row.website)" type="text">
                  {{ scope.row.website }}
                </el-button>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="有效期" align="center" prop="effectiveTime" width="120"  :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.effectiveTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="附件" align="center">
            <template slot-scope="scope">
              <el-button v-if="scope.row.url != null && scope.row.url != ''" @click="handlePictureCardPreview(scope.row.uploadFile)" size="small"
                         type="text">附件</el-button>
              <el-button size="small" v-else type="text">无附件</el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="85px" fixed="right">
            <template slot-scope="scope">
              <el-button  v-if="!scope.row.feedback"  size="mini" type="text" icon="el-icon-sunny" @click="handleInterested(scope.row,scope.$index)"
              title="感兴趣"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
        </div>
        <!-- 添加或修改政策发布对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body @close="cancel">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
            <div v-if="addSign !== 0">
              <el-descriptions title="">
                <el-descriptions-item label="序号">
                  <el-tag size="medium">{{ form.index }}</el-tag>
                </el-descriptions-item>
              </el-descriptions>
            </div>
            <el-form-item label="创建人" prop="creationName" v-if="typeSelect!==2">
              <el-input v-model="form.creationName" placeholder="请输入创建人" disabled="true"/>
            </el-form-item>
            <el-form-item label="创建时间" prop="creationTime" v-if="typeSelect!==2">
              <el-date-picker clearable
                              v-model="form.creationTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择创建时间"
                              style="width: 178px"
                              disabled="true">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="有效期" prop="effectiveTime">
              <el-date-picker clearable
                              v-model="form.effectiveTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择有效期"
                              style="width: 178px"
                              :picker-options="{ disabledDate: dateOptions }"
                              :disabled="ReadOnly"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="政策主题" prop="policyThemes">
              <el-input v-model="form.policyThemes" placeholder="请输入政策主题" :disabled="ReadOnly"/>
            </el-form-item>
            <el-form-item label="区域" prop="region" >
              <Treeselect v-model="form.region" :options="optionsArea"   style="width: 178px"  :flat="true" :show-count="true" :normalizer="normalizer"  placeholder="请选择区域" :disabled="ReadOnly" />
            </el-form-item>
            <el-form-item label="原文网址" prop="website" v-if="typeSelect!==2">
              <el-input v-model="form.website" type="textarea" placeholder="请输入内容" :disabled="ReadOnly" style="width: 440px"  maxlength="200" show-word-limit/>
            </el-form-item>
            <el-form-item label="政策内容" prop="content">
              <quill-editor v-model="form.content" :options="editorOptions" style="width: 700px"  :disabled="ReadOnly" maxlength="300" show-word-limit/>
            </el-form-item>
            <el-form-item label="附件" prop="uploadFile" label-width="110px" v-if="typeSelect!==2">
              <file-view v-if="show" :limit="1" :type="typeSelect" :businessId="form.uploadFile" :businessCode="typeCode"
                         style="width: 500px" ></file-view>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer" v-if="addSign!==2" style="text-align: center;">
            <el-button type="primary" @click="submitForm(form.id)" >审 核</el-button>
            <el-button @click="cancel">关 闭</el-button>
          </div>
        </el-dialog>
        <!--        选择分类群发企业-->
        <el-dialog :title="title" :visible.sync="openUser" width="1400px" append-to-body>
          <el-scrollbar>

            <div class="content-box">
              <BusinessInfo :markAs="1" @change="close" @ok="selectEnterprise" :enterprise="1" :isTree="false" :train="true">
              </BusinessInfo>
            </div>
          </el-scrollbar>
        </el-dialog>
        <!--    附件展示对话框-->
        <el-dialog title="查看附件" :visible.sync="openFileList" width="500px" append-to-body v-if="openFileList">
          <UploadFile :upload-id="uploadId" :business-code="typeCode"></UploadFile>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listRelease, getRelease, receiveList,readStatus,interestStatus } from "@/api/policy/release";
import { listType } from "@/api/maintype/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import MainType from "../../../components/MainType/index.vue";
import BusinessInfo from "../../enterprise/info/index.vue";
import FileView from "@/components/FileView/index";
import UploadFile from "../../upload/index.vue";
import { listArea } from "@/api/area/area";
import { quillEditor } from 'vue-quill-editor' //引入插件
export default {
  name: "review",
  components: {UploadFile, BusinessInfo, MainType,FileView,Treeselect,quillEditor},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      typeSelect: 0,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      typeCode: 'policy',
      // 总条数
      total: 0,
      // 政策发布表格数据
      releaseList: [],
      //行政区域
      optionsArea:null,
      // 弹出层标题
      title: "",
      ReadOnly:false,
      openFileList:false,
      openUser: false,
      addSign:0,
      // 是否显示弹出层
      open: false,
      //图片的标志位
      show: false,
      uploadId:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        policyThemes: null,
        receiverId: null,
        status: null,
        belongTo: null,
        classificationId: null,
        examineId: null,
      },
      //主分类
      options: null,
      // 表单参数
      form: {
        //客户id数组
        enterpriseId: [],
      },
      // 表单校验
      rules: {
      },
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],        //加粗，斜体，下划线，删除线
            ['blockquote', 'code-block'],                     //引用，代码块
            [{ 'header': 1 }, { 'header': 2 }],               // 标题，键值对形式
            [{ 'list': 'ordered'}, { 'list': 'bullet' }],
            [{ 'indent': '-1'}, { 'indent': '+1' }],          // 缩进
            [{ 'direction': 'rtl' }],                         // 文本方向
            [{ 'size': ['small', false, 'large', 'huge'] }], // 字体
            [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
            [{ 'color': [] }, { 'background': [] }],          // 字体颜色，字体背景
            [{ 'font': [] }],
            [{ 'align': [] }],
            ['clean'],                                         // 清除文本格式
  
          ]
        },
        placeholder: '请输入内容...',
        theme: 'snow' // or 'bubble'
      }
    };
  },
  computed: {
    editorOptions() {
      if (this.ReadOnly) {
        return {
          ...this.editorOption,
          modules: {
            ...this.editorOption.modules,
            toolbar: false
          },
          readOnly: true
        };
      }
      return this.editorOption;
    }
  },
  created() {
    this.getList();
    this.area();
    if (this.$route.query.id!==undefined){
      this.form.id=this.$route.query.id;
      this.handleSelect(this.form,1);
    }
  },
  methods: {
    /** 查询政策发布列表 */
    getList() {
      this.loading = true;
      receiveList(this.queryParams).then(response => {
        this.releaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //感兴趣
    handleInterested(row, index) {
      this.$modal.confirm('是否标记为感兴趣？').then(function () {
        return interestStatus(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("标记成功");
      }).catch(() => { });
    },
    //报名截止日期控件
    dateOptions(time) {
      return time.getTime() < Date.now() - 24 * 60 * 60 * 1000
    },
    handleClick(url) {
      if (!url.startsWith('http://') && !url.startsWith('https://')) {
        // 如果没有协议，添加默认的http://协议
        url = 'http://' + url;
      }
      window.open(url, '_blank');
    },
    area() {
      listArea({}).then(response => {
        this.optionsArea = response.data;
      });
    },
    normalizer(node) {
      console.log(node)
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.areaId,
        label: node.areaName,
        isDisabled: node.parentId == '000000' ? true : false,
        children: node.list === undefined ? 0 : node.list,
      }
    },
    // 取消按钮
    cancel() {
      this.show = false;
      this.open = false;
      this.reset();
    },
    close() {
      this.openUser = false
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createId: null,
        policyThemes: null,
        website: null,
        creationName: null,
        effectiveTime: null,
        creationTime: null,
        uploadFile: null,
        receiverId: null,
        receiverName: null,
        operateTime: null,
        status: null,
        operateId: null,
        operateName: null,
        departmentId: null,
        departmentName: null,
        organizationId: null,
        organizationName: null,
        belongTo: null,
        classificationId: null,
        examineId: null,
        sendType: null,
        examineName: null,
        //客户id数组
        enterpriseId: [],
        region: null,
        isFeedback:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //获取当前用户在企业管理中的企业信息
    selectEnterprise(selection) {
      this.form.enterpriseId = selection.map(item => item.enterpriseId);
      this.form.receiverName = selection.map(item => item.enterpriseName).join(',');
      console.log(this.form)
      this.close()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //点击接收方文本域,弹出选择当前用户在企业管理中的企业信息
    handleTextAreaFocus() {
      this.openUser = true;
    },
    /** 查看按钮 */
    handleSelect(row, index) {
      const id = row.id || this.ids
      getRelease(id).then(response => {
        this.form = response.data;
        console.log(this.form.region)
        this.sendTrain();
        this.typeSelect = 2;
        this.addSign = 2;
        this.form.index = index + 1;
        this.ReadOnly = true;
        //把接收方的id转成数组
        this.form.enterpriseId = this.form.receiverId.split(',');
        this.show = true;
        this.open = true;
        this.title = "查看政策发布";
        //标记已读
        readStatus(row.id);
      });
    },
    //当有值的时候，默认打开对话框为分类群发，没值为一键发送
    sendTrain() {
      if (this.form.receiverName !== null) {
        this.sendType = "groupSend"
      } else {
        this.sendType = "oneClick"
      }
    },
    handlePictureCardPreview(id) {
      this.uploadId = id;
      this.openFileList = true;
    },
  }
};
</script>
